<template>
  <!-- 西药处方打印选择 -->
  <el-dialog
    title="西药处方打印"
    :visible="value"
    width="900px"
    custom-class="dialog-box"
    @close="onClose"
    @open="onOpen"
    :close-on-click-modal="false"
    :append-to-body="true"
  >
    <div v-loading="btnLoading">
      <el-table :data="tableList" ref="multipleTable" tooltip-effect="dark" style="width: 100%" @selection-change="onCheckAll">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="药品名称" prop="drugName"></el-table-column>
        <el-table-column label="规格" prop="specs"></el-table-column>
        <el-table-column label="剂量">
          <template slot-scope="scope">{{ scope.row.oneDose }}{{ scope.row.oneDoseUnit | changeAssist(oneDoseUnitArr) }}</template>
        </el-table-column>
        <el-table-column label="用法">
          <template slot-scope="scope">{{ scope.row.use | changeAssist(useArr) }}</template>
        </el-table-column>
        <el-table-column label="频次">
          <template slot-scope="scope">{{ scope.row.frequency | changeAssist(frequencyArr) }}</template>
        </el-table-column>
        <el-table-column prop="useDay" label="天数"></el-table-column>
        <el-table-column label="总量">
          <template slot-scope="scope">{{ scope.row.useTotal }}{{ scope.row.totalUnit | changeAssist(oneDoseUnitArr) }}</template>
        </el-table-column>
      </el-table>
      <!-- <div v-if="tableList.length==0" style="color:#999;padding:10px 0;text-align:center;">
        <i class="iconfont iconwushuju" style="font-size:40px;"></i>
        <div>暂无数据</div>
      </div>-->
    </div>
    <div class="form-tool" style="align-items: center;" slot="footer">
      <div style="text-align:left;">
        <el-checkbox v-model="isOrdinary">普通</el-checkbox>
        <el-checkbox v-model="isFlow">输液单</el-checkbox>
        <el-checkbox v-model="isHocus">麻醉单</el-checkbox>
      </div>
      <div>
        <el-button type="primary" :loading="btnLoading" @click="onSubmit">确 定</el-button>
      </div>
    </div>
    <iframe ref="iframe1" frameborder="0" height="0px" width="0px" src="/print/newwestwardreport.html"></iframe>
  </el-dialog>
</template>
<script>
export default {
  props: ["value", "cureId", "patientSignInfo"],
  data() {
    return {
      printData: null,
      btnLoading: false,
      tableList: [],
      activeList: [],
      userWeight: null,
      isOrdinary: true, //普通处方
      isHocus: false, //麻醉单
      isFlow: false //是否输液单
    };
  },
  computed: {
    //剂量单位
    oneDoseUnitArr() {
      return this.$store.state.doctorMZ.enum.doseUnitEnum;
    },
    //用法
    useArr() {
      return this.$store.state.doctorMZ.enum.useEnum;
    },
    //频次
    frequencyArr() {
      return this.$store.state.doctorMZ.enum.frequencyEnum;
    },
    checkAll() {
      const th = this;
      let bo = true;
      let index = th.tableList.findIndex(d => {
        return !d.check;
      });
      if (index != -1) bo = false;
      return bo;
    }
  },
  mounted() {
    const th = this;
  },
  methods: {
    async onSubmit() {
      let th = this;
      if (!th.isOrdinary && !th.isFlow && !th.isHocus) return;
      if (th.activeList.length == 0) return;
      let json = {
        hospital: {
          name: th.adminUser.hospitalName,
          logo: th.adminUser.printLogo
        },
        enum: {
          unitEnum: th.oneDoseUnitArr,
          useEnum: th.useArr,
          frequencyEnum: th.frequencyArr
        },
        professionalDeptName: th.printData.professionalDeptName,
        // isChild: th.printData.isChild, //是否儿科
        patientInfo: th.printData.basePrintVO,
        time: th.$utils.FormatTime("yyyy-MM-dd", th.printData.westernPrescriptionDrugPrintVO[0].openOrderTime),
        barCode: "", //条形码
        code: th.printData.westernPrescriptionDrugPrintVO[0].code, //处方号
        // allergy: th.tableList.allergy,
        firstVisit: th.printData.firstVisit,
        isFlow: th.isFlow,
        isHocus: th.isHocus,
        isOrdinary: th.isOrdinary,
        doctorName: th.printData.doctorName,
        drug: []
      };

      for (let index = 0; index < th.activeList.length; index++) {
        const element = th.activeList[index];
        if (!element.check) continue;
        //计算价格
        element.sellMoney = element.medicalDrugSale.sellMoney;
        //判断是否拆分
        if (element.medicalDrugSale.isPiece == 1 && element.totalUnit == element.medicalDrugSale.preparationUnit) {
          element.sellMoney = element.medicalDrugSale.pieceMoney;
        }
        json.drug.push(element);
      }
      if (json.drug.length == 0) {
        th.$message.warning("请勾选药品");
        return;
      }
      th.btnLoading = true;
      let res1 = await th.$api.findCureCode({ cureId: th.cureId });
      if (!res1.executed) {
        th.btnLoading = false;
        return;
      }
      th.btnLoading = false;
      json.barCode = th.$config.imageUrl + res1.dataBody.barCode;

      // 2021 FIX by jy
      // 使用findCureCode接口返回的体重
      // json.patientInfo.weight = th.userWeight;
      json.patientInfo.weight = res1.dataBody.bodyWeight;
      th.$refs["iframe1"].contentWindow.postMessage(json, `${window.location.origin}/print/newwestwardreport.html`);
      th.onClose();
    },
    //全选
    onCheckAll(bo) {
      this.activeList = bo;
      // let th = this;
      // th.tableList.forEach((el) => {
      //   th.$set(el, "check", bo);
      // });
    },
    onOpen() {
      let th = this;
      //   let list = JSON.parse(JSON.stringify(th.list));

      //   th.tableList = list.map(d => {
      //     d.check = true;
      //     return d;
      //   });
      let j = {
        cureId: th.cureId
      };
      th.btnLoading = true;
      th.$api
        .printwesterncurePrescription(j)
        .then(res => {
          if (res.executed) {
            th.btnLoading = false;
            th.printData = res.data.data;
            let list = th.printData.westernPrescriptionDrugPrintVO[0].traditionalPrescriptionDrugPrintVOS;
            th.tableList = list.map(d => {
              d.check = true;
              return d;
            });
            this.$nextTick(() => {
              this.tableList.map(obj => {
                this.$refs.multipleTable.toggleRowSelection(obj, true);
              });
            });
            // 2021 FIX by jy
            // 取消获取体重
            getWeight();
          }
        })
        .catch(error => {
          this.btnLoading = false;
        });
      //获取体重
      const getWeight = function() {
        if (th.printData.isChild) {
          th.$api
            .getChildWho({
              patientId: th.printData.basePrintVO.patientId,
              whoType: 1
            })
            .then(res => {
              if (res.executed) {
                if (res.pxInfo && res.pxInfo.length > 0) th.userWeight = res.pxInfo[res.pxInfo.length - 1].weights;
                else th.userWeight = th.patientSignInfo.weight;
              }
            });
        } else {
          th.$api.getPatientSignById({ patientId: th.printData.basePrintVO.patientId }).then(res => {
            if (res.executed) {
              if (Object.keys("patientSign") != undefined) {
                th.userWeight = res.patientSign.weight;
              }
            }
          });
        }
      };
    },
    onClose() {
      let th = this;
      th.tableList = [];
      th.isFlow = false;
      th.isHocus = false;
      th.isOrdinary = true;
      th.$emit("input", false);
    }
  }
};
</script>
